package com.newmainsoftech.aspectjutil.eventmanager.testmodels;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import com.newmainsoftech.aspectjutil.eventmanager.EventInfo;
import com.newmainsoftech.aspectjutil.eventmanager.EventInfo.EventerType;
import com.newmainsoftech.aspectjutil.eventmanager.label.EventListener;
import com.newmainsoftech.aspectjutil.eventmanager.label.OnEvent;

@Aspect
@EventListener
public class EventProducerWrapperAspect {
	@Pointcut( "execution( * com.newmainsoftech.aspectjutil.eventmanager.testmodels.EventProducerWrapper.*(..))")
	public static void pointcutAtEventProducerWrapperMethodsExecution() {}
	
	@Before( value="pointcutAtEventProducerWrapperMethodsExecution()")
	public void beforeEventProducerWrapperMethodsExecution( JoinPoint joinPoint) {
		System.out.println(
				String.format(
						"EventProducerWrapperAspect detected execution of %1$s",
						joinPoint.getSignature().toString()
						)
				);
	}
	
	protected static EventInfo eventInfo;
	public static final EventInfo getEventInfo() {
		return eventInfo;
	}

	@OnEvent( value={ SampleEventClass.PriceChange.class, SampleEventClass.CalculationEvent.class})
	public void onEvent( EventInfo eventInfo) {
		EventProducerWrapperAspect.eventInfo = eventInfo;
		
		System.out.println(
				String.format(
						"Result of %1$s event triggered by %2$s is %3$s",
						eventInfo.getTriggeredEvent().toString(),
						eventInfo.getEventerInfo().getEventerObj().getClass().toString() + "." 
						+ (eventInfo.getEventerInfo().getEventerType().equals( EventerType.Method) ?
								eventInfo.getEventerInfo().getEventerMethod().toString() 
								: eventInfo.getEventerInfo().getEventerField().toString()
								),
								eventInfo.getResult().toString()
						)
				);
	}
}
